package com.FH.login;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 公共方法类
 * @author Zhangsx
 *
 */
public class ToolsUtils {

	private static final String[] IMG_FORMAT = { "jpg" };

	/**
	 * 把传入的字符串转换成日期类型
	 * @param dateStr
	 * @return
	 */
	public static Date getDateFromStr(String dateStr){
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 获取当前传入的时间
	 * @param date
	 * @return
	 */
	public static String getCurrentDateTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return sdf.format(cal.getTime());
	}
	/**
	 * 将传入的时间转换为字符串格式输出
	 * @param date
	 * @return
	 */
	public static String getDateTimeToStr(Date date,int dateLength) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return sdf.format(cal.getTime()).substring(0,dateLength);
	}
	/**
	 * 根据文档名称判断文档类型
	 * @param docName
	 * @return
	 */
	public static String getDocType(String docName){
		String typeStr = docName.substring(docName.lastIndexOf("."),docName.length()).toLowerCase();
		String typePDF = "PDF";
		String typeWord = "WORD";
		String typeExcel = "EXCEL";
		String typePPT = "PPT";
		if(typeStr.equals(".doc")||typeStr.equals(".docx")){
			return typeWord;
		}else if(typeStr.equals(".xls")||typeStr.equals(".xlsx")){
			return typeExcel;
		}else if(typeStr.equals(".ppt")||typeStr.equals(".pptx")){
			return typePPT;
		}else{
			return typePDF;
		}
	}

	/**
     * 封面图片上传前统一修改成120 * 160 大小
     * @param file
     * @param widths
     * @param heights
     * @return
     */
	private static String scale(String file, String result2Img, int widths, int heights) {
		//String newPath = getPath(file);
		try {
			BufferedImage src = ImageIO.read(new File(file)); // 读入文件
			Image image = src.getScaledInstance(widths, heights,
					Image.SCALE_DEFAULT);
			BufferedImage tag = new BufferedImage(widths, heights,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, 0, 0, null); // 绘制缩小后的图
			g.dispose();
			ImageIO.write(tag, "JPEG", new File(result2Img));// 输出到文件流
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result2Img;
	}



    /**
     * 判断是否包含中文
     * @param chineseStr
     * @return
     */
    public static final boolean isChineseCharacter(String chineseStr) {
    	char[] charArray = chineseStr.toCharArray();
    	for (int i = 0; i < charArray.length; i++) {
    		if ((charArray[i] >= 0x4e00) && (charArray[i] <= 0x9fbb)) {
    			return true;
    		}
    	}
    	return false;
	}
}
